package MapReduce;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class Reducer02 extends
		Reducer<Text, IntWritable, Text, String> {
	
	public void reduce(Text key, Iterable<IntWritable> values, Context context)
			throws IOException, InterruptedException {
		SimpleDateFormat hdsdf = new SimpleDateFormat("yyyy/MM");
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, cal.get(Calendar.MONTH) - 1);
		String date = hdsdf.format(cal.getTime());
		String year = date.toString().split("/")[0];
		String month = date.toString().split("/")[1];
		
		int sum = 0;
		for (IntWritable val : values) {
			sum += val.get();
		}
		
		String result = year+"\t"+month+"\t"+String.valueOf(sum);
		
		context.write(key, result);
	}
}